const express = require('express');
const route = express.Router()
const advModel = require("../db/model/advModel") //引入数据库集合
const isLoginMiddle = require('../middleware/isLoginMiddle') //引入权限控制中间件


//查询广告接口
route.get("/advList/:pageNo/:everyNum", isLoginMiddle, async (req, res) => {
    //把页码和每页数据条数取出来
    const {
        pageNo,
        everyNum
    } = req.params;

    try {
        const findResult = await advModel.find({}).limit(everyNum).skip((pageNo - 1) * 3);
        //请求总数量
        const allAdvCount = await advModel.count({})

        //响应数组给前端
        res.send({
            code: 10000,
            msg: "查询成功",
            data: {
                advList: findResult,
                pageCount: Math.ceil(allAdvCount / everyNum)
            }

        })
    } catch (e) {
        console.log("查询广告列表异常" + e.message);
        res.send({
            code: 10001,
            msg: "查询广告列表异常" + e.message,
            data: null
        })
    }

})

module.exports = route